/**
 * 
 */
package leetCode;

import java.util.ArrayList;

/**
 * @author zhongfang
 *
 */
public class StringIterator {
	ArrayList<Character> chars;
	ArrayList<Integer> freq;
    int currentIndex=0;
    public StringIterator(String compressedString) {
    	int n=compressedString.length();
        chars=new ArrayList<>();
        freq=new ArrayList<>();
        for(int i=0;i<compressedString.length();){
            char ch=compressedString.charAt(i);
            int j=i+1;
            StringBuilder digit=new StringBuilder();
            while (j<n&&compressedString.charAt(j)>='0'&&compressedString.charAt(j)<='9') {
				digit.append(compressedString.charAt(j));
				j++;
			}
            int x=Integer.parseInt(digit.toString());
            chars.add(ch);
            freq.add(x);
            i=j;
        }
    }
    
    public char next() {
    	if(freq.get(freq.size()-1)==0){
    		return ' ';
    	}
    	char result=chars.get(currentIndex);
    	freq.set(currentIndex, freq.get(currentIndex)-1);
    	if(freq.get(currentIndex)==0){
    		currentIndex++;
    	}
    	return result;
    	
    }
    
    public boolean hasNext() {
        return freq.get(freq.size()-1)!=0;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StringIterator stringIterator=new StringIterator("L10e2t1C1o1d1e11");
		for (int i = 0; i < 36; i++) {
			System.out.println(stringIterator.next());
			System.out.println(stringIterator.hasNext());
			System.out.println(i+1);
		}
	}

}
